Skip to content

Conversation

@khaledalam
Copy link

Description

Fixes #20871 by improving error messages for invalid -d command-line options.

Previously, when an invalid -d option was passed to PHP CLI, the error message reported "Unknown on line X". This was confusing for users who don't know about PHP's internal INI handling.

image

Changes

  • Modified Zend/zend_ini_parser.y to detect CLI/FPM -d option parsing context
  • When parsing -d options (filename is "Unknown" and unbuffered_errors is true), error messages now report "-d option on line 0" instead of "Unknown on line X"
  • Added test case sapi/cli/tests/gh20871.phpt to verify the fix with multiple invalid syntax scenarios

@iluuu1994
Copy link
Member

It would probably help even more if there was an indication that the parsing error happens for INI parsing, rather than parsing of the PHP code. For example, instead of "in Unknown" it could print "in INI command line parameter '-d'". Or maybe even the INI line itself.

@khaledalam
Copy link
Author

It would probably help even more if there was an indication that the parsing error happens for INI parsing, rather than parsing of the PHP code. For example, instead of "in Unknown" it could print "in INI command line parameter '-d'". Or maybe even the INI line itself.

@iluuu1994 Thank you for the feedback! I've updated the message. The error message now clearly indicates this is INI parsing,

e.g. PHP: syntax error, unexpected '=' in INI command line parameter '-d'

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better if this is also reviewed by somebody else. ^^ @bukka?

error_buf = (char *) emalloc(error_buf_len);

sprintf(error_buf, "%s in %s on line %" PRIu32 "\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
if (strcmp(currently_parsed_filename, "Unknown") == 0 && CG(ini_parser_unbuffered_errors)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to check if (ini_filename) instead. Side-note: The if (currently_parsed_filename) { is useless because zend_ini_scanner_get_filename() never returns NULL.

error_buf = (char *) emalloc(error_buf_len);

sprintf(error_buf, "%s in %s on line %" PRIu32 "\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
if (strcmp(currently_parsed_filename, "Unknown") == 0 && CG(ini_parser_unbuffered_errors)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this check CG(ini_parser_unbuffered_errors)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To distinguish CLI/FPM -d INI overrides from parse_ini_string(). in basic_functions.c it's called with unbuffered_errors = 0, while the other call sites pass 1.

error_buf = (char *) emalloc(error_buf_len);

sprintf(error_buf, "%s in %s on line %" PRIu32 "\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
if (strcmp(currently_parsed_filename, "Unknown") == 0 && CG(ini_parser_unbuffered_errors)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can ini_filename be unset for any other reasons? E.g. when set in php-fpm.conf or php_value in Apache?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unbuffered_errors check correctly identifies CLI/FPM command-line cases. For fpm conf and Apache php_value, I think those use zend_parse_ini_file() which sets ini_filename from the file handle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad error line and file for invalid "-d" arguments

2 participants